<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * posisi.php
 * @author ranuandra
 *
 * 26 Mei 2012 14:21:54
 *
 * 
 */

class Controller_Admin_Posisi extends Kohana_Admin
{
	public function action_index()
	{
		$datas = ORM::factory('mstposisi')->find_all();
		
		$view = View::factory('admin/posisi/index');
		$view->datas = $datas;
		$this->template->container = $view;
	}
	
	public function action_new()
	{
		$form = ORM::factory('mstposisi')->getForm();
		$view = View::factory('admin/posisi/form');
		$view->form = $form;
		$this->template->container = $view;
	}
	
	public function action_edit()
	{
		$id = Request::current()->param('id');
		$data = ORM::factory('mstposisi', $id)->as_array();
		$view = View::factory('admin/posisi/form');
		$view->form = $data;
		$this->template->container = $view;		
	}
	
	
	public function action_save()
	{

		if(isset($_POST['submit']))
		{
			$db = Database::instance();
			$db->begin();
			
			try 
			{
				if(isset($_POST['id']))
				{
					$area = ORM::factory('mstposisi', Request::current()->post('id'));
					$area->posisi = Request::current()->post('posisi');
					$area->save();
				}
				else 
				{
					$area = ORM::factory('mstposisi');
					$area->create_data($_POST);
				}
				
				$db->commit();
				Request::current()->redirect(URL::base().'admin/posisi');
				exit;
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);
			}
		}		
	}
}